{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4d9618c6",
   "metadata": {},
   "source": [
    "* 本学习记录人：许智超\n",
    "* 学习时间：2021-11-23\n",
    "* week13\n",
    "* 参考的文档：\n",
    "> 1.[python官方文档](https://python.org/doc)\n",
    "\n",
    "> 2.[许老师博客](https://cnblogs.com/xuzhichao)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ead83586",
   "metadata": {},
   "source": [
    "# 1. 列表实践之购物车\n",
    "任务（25分钟）\n",
    "\n",
    "来源Directory 2 课后习题：\n",
    "\n",
    "　　购物车程序项目：\n",
    "\n",
    "　　要求：1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "\n",
    "　　   　 2、让用户输入商品编号进行商品的购买。\n",
    "\n",
    "　　  　　3、用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "\n",
    "　　　　  4、用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。\n",
    " \n",
    " 架构：\n",
    "\n",
    "用户输入价格？\n",
    "商品列表 = [\n",
    "\t\t\t[商品1名称，价格，编号？],\n",
    "\t\t\t[商品2名称，价格，编号？]\n",
    "\t\t]\n",
    "\n",
    "打印\n",
    "-----------\n",
    "商品1 编号1 价格1\n",
    "商品2 编号2 价格2\n",
    "商品3 编号3 价格3\n",
    "\n",
    "用户输入编号购买\n",
    "\n",
    "判断1 商品是否存在？\n",
    "\n",
    "判断2 用户余额是否够？ 够的话扣钱\n",
    "\n",
    "结束 ： 用户输入 “退出” 退出购买（while）\n",
    "\t\t打印用户的购买信息，打印余额"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "bb2b586a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的账户余额：1000\n",
      "-----------商品列表----------\n",
      "商品编号：0 ,商品名称：珍珠奶茶, 商品价格：6\n",
      "商品编号：1 ,商品名称：奶盖四季柠檬茶, 商品价格：8\n",
      "商品编号：2 ,商品名称：杨枝甘露, 商品价格：7\n",
      "商品编号：3 ,商品名称：圣代, 商品价格：6\n",
      "商品编号：4 ,商品名称：多肉葡萄, 商品价格：10\n",
      "请输入你要购买的商品编号（输入‘q’退出购买）：1\n",
      "\n",
      "你点的【奶盖四季柠檬茶】已经购买成功，请等候~，您的余额还有【992】元\n",
      "\n",
      "-----------商品列表----------\n",
      "商品编号：0 ,商品名称：珍珠奶茶, 商品价格：6\n",
      "商品编号：1 ,商品名称：奶盖四季柠檬茶, 商品价格：8\n",
      "商品编号：2 ,商品名称：杨枝甘露, 商品价格：7\n",
      "商品编号：3 ,商品名称：圣代, 商品价格：6\n",
      "商品编号：4 ,商品名称：多肉葡萄, 商品价格：10\n",
      "请输入你要购买的商品编号（输入‘q’退出购买）：q\n",
      "-----------购物车----------\n",
      "您的购物车为【[['奶盖四季柠檬茶', 8]]】,您的余额还有【992】\n"
     ]
    }
   ],
   "source": [
    "# 商品信息：列表数据\n",
    "# goods_list[int(choice)] 用户输入之后选择的商品信息\n",
    "goods_list = [\n",
    "    ['珍珠奶茶',6],\n",
    "    ['奶盖四季柠檬茶',8],\n",
    "    ['杨枝甘露',7],\n",
    "    ['圣代',6],\n",
    "    ['多肉葡萄',10]\n",
    "]\n",
    "\n",
    "# 用户购物车信息：列表数据\n",
    "user_shopping_cart = []\n",
    "\n",
    "# 用户输入\n",
    "Alipay = int(input('请输入您的账户余额：'))\n",
    "\n",
    "# 打印商品信息\n",
    "# ---------\n",
    "# 商品编号：%s ,商品名称：%s, 商品价格：%s\n",
    "# 商品编号：%s ,商品名称：%s, 商品价格：%s\n",
    "\n",
    "# print(\"商品编号：%s ,商品名称：%s, 商品价格：%s\"%(0,goods_list[0][0],goods_list[0][1]))\n",
    "# print(\"商品编号：%s ,商品名称：%s, 商品价格：%s\"%(1,goods_list[1][0],goods_list[1][1]))\n",
    "# print(\"商品编号：%s ,商品名称：%s, 商品价格：%s\"%(2,goods_list[2][0],goods_list[2][1]))\n",
    "# print(\"商品编号：%s ,商品名称：%s, 商品价格：%s\"%(3,goods_list[3][0],goods_list[3][1]))\n",
    "# print(\"商品编号：%s ,商品名称：%s, 商品价格：%s\"%(4,goods_list[4][0],goods_list[4][1]))\n",
    "while True: # 考虑什么时候退出\n",
    "    print(\"-----------商品列表----------\")\n",
    "    for i in range(len(goods_list)):\n",
    "        print(\"商品编号：%s ,商品名称：%s, 商品价格：%s\"%(i,goods_list[i][0],goods_list[i][1]))\n",
    "\n",
    "    choice = input(\"请输入你要购买的商品编号（输入‘q’退出购买）：\") # 1. 输入编号数字  2. 输入“q”3. 输入的啥也不是！\n",
    "    if choice.isdigit():\n",
    "        if int(choice)>=0 and int(choice)<len(goods_list):\n",
    "        # 一、数字 在 列表范围内\n",
    "            # 1. 输入编号数字 数值应该在 商品列表范围以内 \n",
    "            # 判断1 ： Alipay  VS  goods_list[choice][1]（商品价格）   goods_list[int(choice)] (用户输入之后选择的商品信息)\n",
    "            if Alipay >= goods_list[int(choice)][1]:\n",
    "                # 1. Alipay重新赋值\n",
    "                Alipay = Alipay - goods_list[int(choice)][1] # 扣钱\n",
    "                print(\"\\n你点的【%s】已经购买成功，请等候~，您的余额还有【%s】元\\n\"%(goods_list[int(choice)][0],Alipay))\n",
    "                user_shopping_cart.append(goods_list[int(choice)] ) # 增加用户选择的商品信息\n",
    "            else:\n",
    "                print(\"\\n你的钱包不够啊，换种方式支付吧~\\n\")\n",
    "        else:\n",
    "        # 二、数字 不在 列表范围内\n",
    "            print(\"您输入的商品不存在，请输入正确的商品编码（输入‘q’退出购买）：\")\n",
    "    elif choice == \"q\":\n",
    "        # 2. 输入“q”\n",
    "        print(\"-----------购物车----------\")\n",
    "        print(\"您的购物车为【%s】,您的余额还有【%s】\"%(user_shopping_cart,Alipay))\n",
    "        break    \n",
    "    else:\n",
    "        # 3.  输入的啥也不是！不符合要求\n",
    "        print(\"**您输入的内容不符合要求，请检查重新输入！**\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "38efdb09",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(goods_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a3e784d",
   "metadata": {},
   "source": [
    "# 2. 请同学们尝试完成实践（二）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f88350f6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hide_card:········\n",
      "guess_digit:3\n",
      "yes, you got it.\n"
     ]
    }
   ],
   "source": [
    "# -*- coding:utf-8 -*-\n",
    "# Author:Zhichao\n",
    "#猜数字（0-100）\n",
    "\n",
    "\n",
    "import getpass\n",
    "hide_card = int(getpass.getpass(\"hide_card:\"))\n",
    "count = 0\n",
    "\n",
    "if hide_card <100 and hide_card >0:\n",
    "    while True:\n",
    "        if count <5:\n",
    "            guess_digit = int(input(\"guess_digit:\"))\n",
    "            if guess_digit == hide_card:\n",
    "                print(\"yes, you got it.\")\n",
    "                break\n",
    "            elif guess_digit > hide_card:\n",
    "                print(\"think smaller...\")\n",
    "            else:\n",
    "                print(\"think bigger...\")\n",
    "            count +=1\n",
    "        else:\n",
    "            print(\"猜这么多次都不对,你个笨蛋!\")\n",
    "            print(\"正确答案：%s\"%(hide_card))\n",
    "            break\n",
    "else:\n",
    "    print(\"请输入数字在0-99之间。不要耍赖！\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66760d85",
   "metadata": {},
   "source": [
    "自己额外扩写的："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2d0af9e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "啊啊，是冒险者嘛。\n",
      "我这里又进了一些新的好货！当然...还有非常可爱的孩子呢❤~\n",
      "===================\n",
      "按a呈列商品列表，按b呈现购买记录，按c退出。\n",
      "输入字母a\n",
      "好的呢\n",
      "下列是武器类\n",
      "1 {'尖刀匕首:150'}\n",
      "==============\n",
      "2 {'激光利刃： 300'}\n",
      "==============\n",
      "3 {'勿活斩： 890'}\n",
      "==============\n",
      "4 {'绝对双枪: 1080'}\n",
      "==============\n",
      "下列是装饰类\n",
      "1 {'防弹衣：350'}\n",
      "==============\n",
      "2 {'三点式护甲：560'}\n",
      "==============\n",
      "3 {'烁金护腕：80'}\n",
      "==============\n",
      "你想要买的是什么呢？武器类还是装备类呢？~\n",
      "请输入你想要购买的装备类型：装备类\n",
      "请输入你想要购买的物品数字：1\n",
      "好的\n",
      "{'三点式护甲': '560'}\n",
      "好的，你所购买的物品价格为 560\n",
      "冒昧一下，虽然听起来没那么好听.....就是如果可以的话我能问一下你的金钱数额吗？\n",
      "那啥，您有多少钱？：1000\n",
      "560\n",
      "<class 'int'>\n",
      "哎哟，很不错哟，小老板挺阔气的嘛\n",
      "好，亲爱的朋~友~，你还剩 440 元\n",
      "所以，你是想再买些什么？还是说现在就走？\n",
      "按a呈列商品列表，按b呈现购买记录，按c退出。\n",
      "输入字母b\n",
      "啊，你买了这个，就是 {'三点式护甲': '560'}\n",
      "输入字母c\n",
      "好的，再见！下次光临~冒险者\n"
     ]
    },
    {
     "ename": "SystemExit",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "An exception has occurred, use %tb to see the full traceback.\n",
      "\u001b[0;31mSystemExit\u001b[0m\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/srv/conda/envs/notebook/lib/python3.6/site-packages/IPython/core/interactiveshell.py:3351: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.\n",
      "  warn(\"To exit: use 'exit', 'quit', or Ctrl-D.\", stacklevel=1)\n"
     ]
    }
   ],
   "source": [
    "print(\"啊啊，是冒险者嘛。\")\n",
    "print(\"我这里又进了一些新的好货！当然...还有非常可爱的孩子呢❤~\")\n",
    "print(\"===================\")\n",
    "print(\"按a呈列商品列表，按b呈现购买记录，按c退出。\")\n",
    "import sys\n",
    "buying = 0\n",
    "\n",
    "\n",
    "def statics(self):\n",
    "    good = {'武器类': [{'尖刀匕首': '150'},\n",
    "                    {'激光利刃': '390'},\n",
    "                    {'勿活斩': '890'},\n",
    "                    {'绝对双枪': '1080'}],\n",
    "            '装备类': [{'防弹衣': '350'},\n",
    "                    {'三点式护甲': '560'},\n",
    "                    {'烁金护腕': '80'}]\n",
    "            }\n",
    "    pints = input(\"请输入你想要购买的装备类型：\")\n",
    "    number = input(\"请输入你想要购买的物品数字：\")\n",
    "    if number.isdigit() is True:\n",
    "        num = int(number)\n",
    "        if pints not in ['武器类', '装备类']:\n",
    "            print(\"大哥大，输错了，再输一次啦！\")\n",
    "            statics('miku')\n",
    "        else:\n",
    "            print(\"好的\")\n",
    "            global buying\n",
    "            buying = good[pints][num]\n",
    "            print(buying)\n",
    "            while True:\n",
    "                if '尖刀匕首' in buying:\n",
    "                    print(\"好的，你所购买的物品价格为\", buying['尖刀匕首'])\n",
    "                    price = buying['尖刀匕首']\n",
    "                    break\n",
    "                elif '激光利刃' in buying:\n",
    "                    print(\"好的，你所购买的物品价格为\", buying['激光利刃'])\n",
    "                    price = buying['激光利刃']\n",
    "                    break\n",
    "                elif '勿活斩' in buying:\n",
    "                    print(\"好的，你所购买的物品价格为\", buying['勿活斩'])\n",
    "                    price = buying['勿活斩']\n",
    "                    break\n",
    "                elif '绝对双枪' in buying:\n",
    "                    print(\"好的，你所购买的物品价格为\", buying['绝对双枪'])\n",
    "                    price = buying['绝对双枪']\n",
    "                    break\n",
    "                elif '防弹衣' in buying:\n",
    "                    print(\"好的，你所购买的物品价格为\", buying['防弹衣'])\n",
    "                    price = buying['防弹衣']\n",
    "                    break\n",
    "                elif '三点式护甲' in buying:\n",
    "                    print(\"好的，你所购买的物品价格为\", buying['三点式护甲'])\n",
    "                    price = buying['三点式护甲']\n",
    "                    break\n",
    "                elif '烁金护腕' in buying:\n",
    "                    print(\"好的，你所购买的物品价格为\", buying['烁金护腕'])\n",
    "                    price = buying['烁金护腕']\n",
    "                    break\n",
    "                else:\n",
    "                    print(\"你输入错了格式，再来一遍吧\")\n",
    "                    statics('miku')\n",
    "            print(\"冒昧一下，虽然听起来没那么好听.....就是如果可以的话我能问一下你的金钱数额吗？\")\n",
    "            money = int(input(\"那啥，您有多少钱？：\"))\n",
    "            print(price)\n",
    "            dict_price = str(price)\n",
    "            init_price = int(dict_price)\n",
    "            print(type(init_price))\n",
    "            if money >= init_price:\n",
    "                print(\"哎哟，很不错哟，小老板挺阔气的嘛\")\n",
    "                buy = money - init_price\n",
    "                print(\"好，亲爱的朋~友~，你还剩\", buy, \"元\")\n",
    "                if 600 <= init_price <= 1000:\n",
    "                    print('良心商人表示向给你打个3折')\n",
    "                    buy = init_price * 0.3\n",
    "                    print(\"好，现在的价格是\", buy, \"欢迎大佬下次再来啊\")\n",
    "                    buy = money - init_price\n",
    "                    print(\"好，亲爱的朋~友~，你还剩\", buy, \"元\")\n",
    "                elif init_price > 1000:\n",
    "                    print('良心商人表示向给你打个8折')\n",
    "                    buy = init_price * 0.8\n",
    "                    print(\"好，现在的价格是\", buy, \"欢迎大佬下次再来啊\")\n",
    "                    buy = money - init_price\n",
    "                    print(\"好，亲爱的朋~友~，你还剩\", buy, \"元\")\n",
    "            else:\n",
    "                print(\"求求啦，氪金后再玩个游戏啦，别在这丢脸啦~\")\n",
    "            print(\"所以，你是想再买些什么？还是说现在就走？\")\n",
    "            print(\"按a呈列商品列表，按b呈现购买记录，按c退出。\")\n",
    "\n",
    "    else:\n",
    "        print(\"输入的是数字，数字啊！\")\n",
    "        statics('miku')\n",
    "\n",
    "    if '__name__' == '__main__':\n",
    "        statics('miku')\n",
    "\n",
    "\n",
    "def listing():\n",
    "    goods = {'武器类': [{'尖刀匕首:150'},\n",
    "             {'激光利刃： 300'},\n",
    "                    {'勿活斩： 890'},\n",
    "                    {'绝对双枪: 1080'}],\n",
    "             '装备类': [{'防弹衣：350'},\n",
    "                     {'三点式护甲：560'},\n",
    "                     {'烁金护腕：80'}]\n",
    "             }\n",
    "    print(\"下列是武器类\")\n",
    "    for i in range(4):\n",
    "        c = i + 1\n",
    "        print(c, goods['武器类'][i])\n",
    "        print(\"==============\")\n",
    "    print(\"下列是装饰类\")\n",
    "    for i in range(3):\n",
    "        d = i + 1\n",
    "        print(d, goods['装备类'][i])\n",
    "        print(\"==============\")\n",
    "\n",
    "\n",
    "while True:\n",
    "    request = input(\"输入字母\")\n",
    "    if request in ['a', 'b', 'c']:\n",
    "        if request == 'a':\n",
    "            print(\"好的呢\")\n",
    "            listing()\n",
    "            print(\"你想要买的是什么呢？武器类还是装备类呢？~\")\n",
    "            statics(self='Miku')\n",
    "        elif request == 'b':\n",
    "            if buying == 0:\n",
    "                print(\"你还什么都没买呢！看什么看！这里没有霸王餐呐！\")\n",
    "            else:\n",
    "                print(\"啊，你买了这个，就是\", buying)\n",
    "        elif request == 'c':\n",
    "            print(\"好的，再见！下次光临~冒险者\")\n",
    "            sys.exit()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c2c2a39f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
